<template>
  <el-container class="he-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="loginout">退出</el-button>
    </el-header>
    <!-- 内容区域 -->
    <el-container>
      <!-- 左侧侧边栏 -->
      <el-aside width="isCollapse ? '64px' : '200px'">
        <div class="asideCe" @click="asideCe">|||</div>
        <el-menu
          :collapse="isCollapse"
          :collapse-transition="false"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          unique-opened
          :router="true"
          :default-active="activePath"
        >
          <el-submenu :index="item.id + ''" :key="item.id" v-for="item in menulist">
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单栏 -->
            <el-menu-item
              :index="'/' + itemSet.path"
              v-for="itemSet in item.children "
              :key="itemSet.id"
              @click="saveNavState('/' + itemSet.path)"
            >
              <i cleass="el-icon-menu"></i>
              <span>{{itemSet.authName}}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容显示区域 -->
      <el-main>
        <!-- 右侧占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>


<script>
export default {
  data() {
    return {
      menulist: [],
      iconObj: {
        '125': 'iconfont icon-users',
        '103': 'iconfont icon-tijikongjian ',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-users'
      },
      // 是否折叠
      isCollapse: false,
      activePath: ''
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    loginout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList() {
      const { data: res } = await this.$http.get('/menus')
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
    },

    //点击是否折叠侧边栏的收起与展开
    asideCe() {
      this.isCollapse = !this.isCollapse
    },
    //点击二级侧边栏高亮
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>


<style lang='less' scoped>
/*父级 样式 */
.he-container {
  height: 100%;
}
/* 头部样式 */
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0px;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}

/* 左侧侧边栏 */
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}

/* 右侧内容样式 */
.el-main {
  background-color: #eaedf1;
}
/* 一级菜单字体图标的间距 */
.iconfont {
  margin-right: 10px;
}
.asideCe {
  background-color: #4a5064;
  color: #fff;
  font-size: 10px;
  text-align: center;
  line-height: 24px;
  letter-spacing: 0.2em;
  // cursor: pointer;
  cursor: pointer;
}
</style>